﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace CoNatural.Data.VisualStudio.AddIn.Controls {
   public partial class SelectConnectionControl : UserControl {
		private bool listChanged;

      public SelectConnectionControl() {
         InitializeComponent();
			LoadCachedConnections();
			listChanged = false;
			this.Disposed += new EventHandler(SelectConnectionControl_Disposed);
      }

		void SelectConnectionControl_Disposed(object sender, EventArgs e) {
			if(listChanged)
				SaveCachedConnections();
		}

      public string GetSelectedConnectionString() {
         if (connectionsComboBox.Items.Count > 0 && connectionsComboBox.SelectedItem != null) {
            return connectionsComboBox.SelectedItem.ToString();
         }
         // no selection
         return null;
      }

      private void newButton_Click(object sender, EventArgs e) {
         using (Microsoft.Data.ConnectionUI.DataConnectionDialog dataConnectionDialog = new Microsoft.Data.ConnectionUI.DataConnectionDialog()) {
            Microsoft.Data.ConnectionUI.DataSource.AddStandardDataSources(dataConnectionDialog);
            dataConnectionDialog.SelectedDataSource = Microsoft.Data.ConnectionUI.DataSource.SqlDataSource;
            dataConnectionDialog.SelectedDataProvider = Microsoft.Data.ConnectionUI.DataProvider.SqlDataProvider;
            if (Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(dataConnectionDialog) == System.Windows.Forms.DialogResult.OK)
					UpdateConnections(dataConnectionDialog.ConnectionString);
         }
      }

		private void connectionsComboBox_Leave(object sender, EventArgs e) {
			UpdateConnections(connectionsComboBox.Text);
		}

		private readonly static string cacheFilePath = "CoNatural";
		private readonly static string cacheFileName = "ConnectionStrings.txt";

		private void UpdateConnections(string connectionString) {
         if (!string.IsNullOrEmpty(connectionString)) {
				if(connectionsComboBox.Items.Count > 0) {
					string selectedItem = (string)connectionsComboBox.Items[0];
					if(selectedItem != null && selectedItem == connectionString)
						return; // don't update if already selected
				}

            if (connectionsComboBox.Items.Contains(connectionString))
               connectionsComboBox.Items.Remove(connectionString);
            connectionsComboBox.Items.Insert(0, connectionString);
            connectionsComboBox.SelectedItem = connectionString;
            listChanged = true;
         }
		}

		private void LoadCachedConnections() {
			try {
				string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
				string filePath = Path.Combine(Path.Combine(path, cacheFilePath), cacheFileName);
				if(File.Exists(filePath)) {
					string[] lines = File.ReadAllLines(filePath);
					foreach(string line in lines)
						if(!string.IsNullOrEmpty(line))
							connectionsComboBox.Items.Add(line);
				}
				if(connectionsComboBox.Items.Count > 0)
					connectionsComboBox.SelectedIndex = 0;
			}
			catch {}
		}

		internal void SaveCachedConnections() {
			try {
				string[] lines = new string[connectionsComboBox.Items.Count];
				connectionsComboBox.Items.CopyTo(lines, 0);

				string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
				string cachePath = Path.Combine(path, cacheFilePath);
				string filePath = Path.Combine(cachePath, cacheFileName);

				if(!Directory.Exists(cachePath))
					Directory.CreateDirectory(cachePath);

				File.WriteAllLines(filePath, lines);
			}
			catch { }
		}
   }
}
